package com.example.eccdemo;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;

import org.spongycastle.asn1.ocsp.Signature;
import org.spongycastle.jce.ECNamedCurveTable;
import org.spongycastle.jce.interfaces.ECPrivateKey;
import org.spongycastle.jce.interfaces.ECPublicKey;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.spongycastle.jce.spec.ECNamedCurveParameterSpec;
import org.spongycastle.util.io.pem.PemObject;
import org.spongycastle.util.io.pem.PemReader;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.X509EncodedKeySpec;
import java.util.Map;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyAgreement;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class MainActivity extends AppCompatActivity {

	private static final String TAG = "MainActivity";

	static {
		Security.addProvider(new BouncyCastleProvider());
	}

	public static byte[] iv = new SecureRandom().generateSeed(16);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Reader rdr = new StringReader("-----BEGIN PUBLIC KEY-----\n"
			+ "MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBhsFCcWY2GaiN1BjPEd1v+ESKO6/0\n"
			+ "D0sUR4y1amHnOr3FZx6TdqdoSBqxownQrnAKGCwagGxUb7BWwPFgHqKQJHgBq+J7\n"
			+ "F+6m5SKAEL1wS5pqya91N7oudF3yFW8oZRE4RQRdSLl3fV2aVXKwGDXciwhUhw8k\n"
			+ "x5OS4iZpMAY+LI4WVGU=\n" + "-----END PUBLIC KEY-----\n");


		try {
		/*	PemObject spki  = new PemReader(rdr).readPemObject();
			PublicKey key = KeyFactory.getInstance("EC","SC").generatePublic(new X509EncodedKeySpec(spki.getContent()));
			System.out.println (key.getAlgorithm() + " " + ((ECPublicKey)key).getQ().toString());


			ECC ecc=new ECC();
			String publicKay="MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEU2/Yf5+GL4zeMCuuYGNQS2nmD/L3L6dGNbCvMkxH8+NYTUAmUYWhIZ/YDOQWkkJxJtUrRUsrsaMbp9QEL/Kfpg==";
			ecc.loadPublicKey(ECC.curve,((ECPublicKey) key).getQ().toString().getBytes());*/
			KeyPair keyPair = KeyUtil.createKeyPairGenerator();
		/*	KeyUtil.savePrivateKeyAsPEM(keyPair.getPrivate(), "privatekey.pem", this);
			KeyUtil.savePublicKeyAsPEM(keyPair.getPublic(), "publickey.pem", this);*/


	/*		ECPublicKey ecPublicKey = ECCUtil.string2PublicKey(new String(Base64.encode(keyPair.getPublic().getEncoded(), 0)));
			String s = new String(ECCUtil.publicEncrypt("Hello World!".getBytes(),ecPublicKey));
			Log.e(TAG,"s---->" + s);

			ECPrivateKey ecPrivateKey = ECCUtil.string2PrivateKey(new String(Base64.encode(keyPair.getPrivate().getEncoded(), 0)));
			String s1 = new String(ECCUtil.privateDecrypt(s.getBytes(), ecPrivateKey));
			Log.e(TAG,"s1---->" + s1);*/


			String publicKeyStr = ECCUtil.getPublicKey(keyPair);
			String privateKeyStr = ECCUtil.getPrivateKey(keyPair);
			System.out.println("ECC公钥Base64编码:" + publicKeyStr);
			System.out.println("ECC私钥Base64编码:" + privateKeyStr);

			ECPublicKey publicKey = ECCUtil.string2PublicKey(publicKeyStr);
			ECPrivateKey privateKey = ECCUtil.string2PrivateKey(privateKeyStr);

			byte[] publicEncrypt = ECCUtil.publicEncrypt("Hello World!".getBytes(), publicKey);
			String encodeToString = ECCUtil.Base64Utils.encodeToString(publicEncrypt);
			System.out.println(encodeToString);
			byte[] privateDecrypt = ECCUtil.privateDecrypt(publicEncrypt, privateKey);
			System.out.println(new String(privateDecrypt));
		} catch (Exception e) {
			Log.e(TAG, "---->" + e.getLocalizedMessage());
			e.printStackTrace();
		}
	}


}